#ifndef AABB_H
#define AABB_H

#include<QVector3D>
#include<ray.h>
#include<iostream>
class aabb
{
public:
    aabb();
    aabb(const QVector3D&a,const QVector3D&b)
    {
        _min=a;
        _max=b;
    }

    QVector3D min()const{return _min;}
    QVector3D max()const{return _max;}
    bool hit(const ray&r,float tmin,float tmax)const{
        for(int a=0;a<3;a++)
        {
//            float t0=ffmin((_min[a]-r.origin()[a])/r.direction()[a],(_max[a]-r.origin()[a])/r.direction()[a]);
//            float t1=ffmax((_min[a]-r.origin()[a])/r.direction()[a],(_max[a]-r.origin()[a])/r.direction()[a]);
//            tmin=ffmax(t0,tmin);
//            tmax=ffmin(t1,tmax);
            float invD=1.0f/r.direction()[a];
            float t0=(min()[a]-r.origin()[a])*invD;
            float t1=(max()[a]-r.origin()[a])*invD;
            if(invD<0.0f)
            std::swap(t0,t1);
            tmin=t0>tmin?t0:tmin;
            tmax=t1<tmax?t1:tmax;
            if (tmax<=tmin)
                    return false;
        }
        return true;
    }
private:
    QVector3D _min;
    QVector3D _max;
};

#endif // AABB_H
